var dateUtils = require('../../utils/dateUtil.js');
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectIndex:0,
    beginTime:'',
    endTime:'',
    switchList:[
      {title:'未完成作业',data:{currPage: 1,list: [],pageSize: 10,totalCount: 0,totalPage: 0},beginTime:'2022-05-23 00:00:00',endTime:'2022-06-23 23:59:59',type:0},
      {title:'已完成作业',data:{currPage: 1,list: [],pageSize: 10,totalCount: 0,totalPage: 0},beginTime:'2022-05-23 00:00:00',endTime:'2022-06-23 23:59:59',type:1},
    ],
    currentLoginInfo:{},

    totalPage:0,
    pageNo:1,
    pageSize:10,
    isLoading:false,
    ifCommit:false,//是否提交作业成功
    clickIndex:0 //点击的位置
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "体育作业"
    })
    this.setData({
      currentLoginInfo:wx.getStorageSync('userData').list[wx.getStorageSync('stuIndex')]
    })

    this.showDate();
    this.getData()
  },

  getData(cd){

    var vt = this.data.switchList[this.data.selectIndex]
    var data = {
      beginTime:vt.beginTime,
      endTime:vt.endTime,
      type:vt.type,
      studentId:this.data.currentLoginInfo.studentId,
      pageNo:this.data.pageNo,
      pageSize:this.data.pageSize,
    }
    console.log('家长作业列表接口数据='+JSON.stringify(data))
    wx.showLoading({
      title: '数据加载中...',
      icon: 'none',
    })
    this.setData({
      isLoading:true
    })
    var that = this;
    app.api.homeworkList(data).then(rest => {
      wx.hideLoading()
      this.setData({
        isLoading:false
      })
      console.log('接口返回='+JSON.stringify(rest))
      cd && cd()
      if(rest&&rest.success){   
        // var vst  =  that.data.switchList;
        // vst[that.data.selectIndex].data =rest.data

        var vst  =  that.data.switchList;
        var dataList = [...vst[that.data.selectIndex].data.list,...rest.data.list]
        vst[that.data.selectIndex].data =rest.data
        vst[that.data.selectIndex].data.list = dataList
    
        that.setData({
          switchList:vst,
          totalPage:rest.data.totalCount
        })
        console.log('接口返回switchList='+JSON.stringify(that.data.switchList[that.data.selectIndex].data.list))
      }else{
        wx.showToast({
          title: rest.errMsg||'网络异常,请重试！',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
    .catch(r => {
      console.error(r)

      wx.showToast({
        title: '登录失败,请重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  },

  showDate(){
    var starTiem = dateUtils.mathChangeDate(dateUtils.formatTime(new Date()),"-",7,true,'')
    var dateCom =  this.selectComponent("#date-select");
    dateCom.onShowAlert(starTiem+" 00:00:00",null);

    var endTime = dateCom.data.endTimeStr 
    var beginTime = dateCom.data.startTimeStr ;
    var list = this.data.switchList;
    for(var i=0;i<list.length;i++){
      var item = list[i];
      item.beginTime = beginTime
      item.endTime  = endTime
    }
    this.setData({
      switchList:list,
      beginTime:beginTime,
      endTime:endTime
    })

    console.log("时间 switchList=="+this.data.beginTime+"==="+this.data.endTime+"==="+JSON.stringify(this.data.switchList));
  },

  //tab切换
  didClickIndex(e){
    let index = e.detail;
    var listData = this.data.switchList;
    listData[index].data.list = []
    console.log('打印：'+index)
    this.setData({
      selectIndex:index,
      pageNo:1,
      switchList:listData,
      totalPage:0
  })
    this.getData()

  },

  goDetail(e){
    var index  = e.currentTarget.dataset.index
    var data = this.data.switchList[this.data.selectIndex].data.list[index];
    this.setData({
      clickIndex:index
    })
    console.log(JSON.stringify(data)+"作业详情33==="+index+"=="+data.isFinish);
    if(this.data.selectIndex==0 && this.data.switchList[this.data.selectIndex].data.list[index].isFinish!=1){//编辑
    wx.navigateTo({
      url: '../../pages/record/parentsWorkDetail?isEdit=1&info='+JSON.stringify(this.data.switchList[this.data.selectIndex].data.list[index]),
      })
    }else{
      wx.navigateTo({
        url: '../../pages/record/parentsWorkDetail?isEdit=0&info='+JSON.stringify(this.data.switchList[this.data.selectIndex].data.list[index]),
      })
    }
  },

  //结束时间按钮确认
  bindTimeChangeEnd(val){
    let repTime = val.detail.replace(/-/g, '/');
    let timeTamp = Date.parse(repTime); 
    let starTime = Date.parse(this.data.beginTime); 
    console.log(timeTamp+' Endpicker发送选择改变，携带值为'+ val.detail+"==="+starTime)
    if(timeTamp>=starTime){
      var list = this.data.switchList;
      for(var i=0;i<list.length;i++){
        var item = list[i];
        item.endTime = val.detail
      }
      list[this.data.selectIndex].data.list = []
      this.setData({
        switchList:list,
        endTime:val.detail,
        pageNo:1,
       
        totalPage:0
      })
      console.log("End确认时间End==="+JSON.stringify(this.data.switchList))
      this.getData()
    }else{
      console.log("时间 switchList=="+this.data.beginTime+"==="+this.data.endTime);
      var dateCom =  this.selectComponent("#date-select");
      dateCom.onShowAlert(this.data.beginTime,this.data.endTime);
      wx.showToast({
        title: '结束时间需大于开始时间',
        icon: 'none',
        duration: 1500,
        mask:true
      })
    }
  },

  //开始时间按钮确认
  bindTimeChangeStart(val){
    let repTime = val.detail.replace(/-/g, '/');
    let timeTamp = Date.parse(repTime); 
    let endTime = Date.parse(this.data.endTime); 
    console.log(timeTamp+' Startpicker发送选择改变，携带值为'+ val.detail+"==="+endTime)
    if(timeTamp<=endTime){
      var list = this.data.switchList;
      for(var i=0;i<list.length;i++){
        var item = list[i];
        item.beginTime = val.detail
      }
      list[this.data.selectIndex].data.list = []
      this.setData({
        switchList:list,
        beginTime:val.detail,
        pageNo:1,
       
        totalPage:0
      })
      console.log("Start确认时间Start==="+JSON.stringify(this.data.switchList))
      this.getData()
    }else{
      console.log("时间 switchList=="+this.data.beginTime+"==="+this.data.endTime);
      var dateCom =  this.selectComponent("#date-select");
      dateCom.onShowAlert(this.data.beginTime,this.data.endTime);
      wx.showToast({
        title: '开始时间需小于结束时间',
        icon: 'none',
        duration: 1500,
        mask:true
      })

    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      console.log("提交成功==="+this.data.ifCommit);
      if(this.data.ifCommit){
        var data = this.data.switchList
        data[this.data.selectIndex].data.list[this.data.clickIndex].isFinish = 1;
        this.setData({
          switchList:data
        })

      }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  

    // 加载更多
    loadmore({
      detail
    }) {
      
      console.log('加载更多='+this.data.pageSize * this.data.pageNo)
      //TODO 修改
  
      var dataList  =  this.data.switchList;
  
     
     var data = dataList[this.data.selectIndex].data;
     var totalCount = data.totalCount
  
  
     if(data.currPage*data.pageSize >= totalCount){
  

      data.currPage =totalCount
     
        wx.showToast({
          title: '暂无更多记录！',
          icon: 'none',
        })
      this.setData({
        isLoading:false
      })

      detail.success();

    
      return
    }
   
  
      if(this.data.isLoading){
        return
      }
  
      this.setData({
        pageNo:this.data.pageNo + 1
      })
      this.getData(()=>{
        detail.success();
      })
    },
    // 刷新
    refresh({
      detail
    }) {
      console.log('下拉刷新='+this.data.pageSize * this.data.pageNo)
  
      var dataList  =  this.data.switchList;
  
     
      dataList[this.data.selectIndex].data.list = []
  
      this.setData({
        pageNo:1,
        switchList:dataList,
        totalCount:0,
        loading: false,
        allloaded: false,
      })
      this.getData(()=>{
        detail.success();
      })
    },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})